package com.msmall.community.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.util.ConstantDB;
import com.msmall.community.ApplicationManager;
import com.msmall.community.common.model.Message;
import com.msmall.community.team.model.Team;
import com.msmall.community.team.model.TeamUser;
import com.msmall.community.team.service.TeamManager;
import com.msmall.community.user.model.UserInfo;
import com.msmall.community.user.service.UserManager;

public class SysMessageTag extends TagSupport{

	private static final long serialVersionUID = 1789753724652018254L;
	
	private static final Log log = LogFactory.getLog(SysMessageTag.class);
	
	private Message message;
	
	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}

	public int doEndTag() throws JspException {
		JspWriter writer = pageContext.getOut();
		HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
		HttpSession session = req.getSession();
		StringBuilder b = new StringBuilder();
		try{
			if(ConstantDB.MESSAGE_TYPES_ROSTER_REQUEST.compareTo(message.getTypes()) == 0){
				b = getRosterRequest(req,session);
			} else if(ConstantDB.MESSAGE_TYPES_ROSTER_ACCEPT.compareTo(message.getTypes()) == 0){
				b = getRosterAccept(req,session);
			} else if(ConstantDB.MESSAGE_TYPES_ROSTER_REJECT.compareTo(message.getTypes()) == 0){
				b = getRosterReject(req,session);
			} else if(ConstantDB.MESSAGE_TYPES_TEAM_INVITE.compareTo(message.getTypes()) == 0){
				b = getTeamInvite(req,session);
			} else if(ConstantDB.MESSAGE_TYPES_TEAM_JOIN.compareTo(message.getTypes()) == 0){
				b = getTeamJoin(req,session);
			} else if(ConstantDB.MESSAGE_TYPES_TEAM_JOIN_ACCEPT.compareTo(message.getTypes()) == 0){
				b = getTeamJoinAccept(req,session);
			} else if(ConstantDB.MESSAGE_TYPES_TEAM_JOIN_REJECT.compareTo(message.getTypes()) == 0){
				b = getTeamJoinReject(req,session);
			} else if(ConstantDB.MESSAGE_TYPES_TEAM_LEAVE_USER.compareTo(message.getTypes()) == 0){
				b = getLeaveTeamUser(req,session);
			} else if(ConstantDB.MESSAGE_TYPES_TEAM_POSITION.compareTo(message.getTypes()) == 0){
				b = getTeamPosition(req,session);
			}
		}catch(Exception e){
			log.error(e);
		}
		try {
			writer.print(b.toString());
		} catch (IOException e) {
			log.error(e);
		}
		return (SKIP_BODY);
	}
	/**
	 * 好友请求通过后，告诉发送人消息
	 */
	private StringBuilder getRosterAccept(HttpServletRequest req, HttpSession session){
		String jsessionid = session.getId();
		String cpath = req.getContextPath();
		StringBuilder b = new StringBuilder();
		UserManager umanager = (UserManager) ApplicationManager.appContext.getBean("userManager");
		UserInfo sender = umanager.getByUserid(message.getSender());
		b.append("(已通过)好友请求：")
		.append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/space/index.do;jsessionid=" + jsessionid +"?usr=" +sender.getUserid()+"\">")
		.append(sender.getNickname())
		.append("</a>")
		.append("通过您的好友请求.")
		.append("&nbsp;&nbsp;").append(getDeleteHref(message.getMessageid()));
		
		return b;
	}
	/**
	 * 好友请求拒绝后，告诉发送人消息
	 */
	private StringBuilder getRosterReject(HttpServletRequest req, HttpSession session){
		String jsessionid = session.getId();
		String cpath = req.getContextPath();
		StringBuilder b = new StringBuilder();
		UserManager umanager = (UserManager) ApplicationManager.appContext.getBean("userManager");
		UserInfo sender = umanager.getByUserid(message.getSender());
		b.append("(拒绝)好友请求：")
		.append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/space/index.do;jsessionid=" + jsessionid +"?usr=" +sender.getUserid()+"\">")
		.append(sender.getNickname())
		.append("</a>")
		.append("拒绝了您的好友请求.")
		.append("&nbsp;&nbsp;").append(getDeleteHref(message.getMessageid()));
		
		return b;
	}
	/**
	 * 生成好友请求消息
	 */
	private StringBuilder getRosterRequest(HttpServletRequest req, HttpSession session){
		String jsessionid = session.getId();
		String cpath = req.getContextPath();
		StringBuilder b = new StringBuilder();
		UserManager umanager = (UserManager) ApplicationManager.appContext.getBean("userManager");
		UserInfo sender = umanager.getByUserid(message.getSender());
		if(message.getStatus().compareTo(ConstantDB.UNREAND) == 0){
			b.append("(未处理)好友请求：")
			.append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/space/index.do;jsessionid=" + jsessionid +"?usr=" +sender.getUserid()+"\">")
			.append(sender.getNickname())
			.append("</a>")
			.append("请求加您为好友:").append(message.getContent())
			.append("&nbsp;&nbsp;<a href=\"javascript:acceptRosterRequest("+sender.getUserid()+ "," + message.getMessageid()+")\">").append("同意").append("</a>")
			.append("&nbsp;&nbsp;<a href=\"javascript:rejectRosterRequest("+sender.getUserid()+ "," + message.getMessageid()+")\">").append("拒绝").append("</a>");
		}else{
			b.append("(已处理)好友请求：")
			.append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/space/index.do;jsessionid=" + jsessionid +"?usr=" +sender.getUserid()+"\">")
			.append(sender.getNickname())
			.append("</a>")
			.append("请求加您为好友:").append(message.getContent())
			.append("&nbsp;&nbsp;").append(getDeleteHref(message.getMessageid()));
		}
		
		return b;
	}
	/**
	 * 生成群组邀请消息
	 */
	private StringBuilder getTeamInvite(HttpServletRequest req, HttpSession session){
		String jsessionid = session.getId();
		String cpath = req.getContextPath();
		StringBuilder b = new StringBuilder();
		UserManager umanager = (UserManager) ApplicationManager.appContext.getBean("userManager");
		TeamManager tmanager = (TeamManager) ApplicationManager.appContext.getBean("teamManager");
		UserInfo sender = umanager.getByUserid(message.getSender());
		Team team = (Team) tmanager.get(Team.class, Long.valueOf(message.getContent()));
		if(team.getStatus().compareTo(Team.STATUS_NORMAL) != 0){
			return b;
		}
		if(message.getStatus().compareTo(ConstantDB.UNREAND) == 0){
			b.append("(未处理)群组邀请：")
			.append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/space/index.do;jsessionid=" + jsessionid +"?usr=" +sender.getUserid()+"\">")
			.append(sender.getNickname())
			.append("</a>")
			.append("邀请您加入群组").append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/team/index.do;jsessionid=" + jsessionid +"?teamid=" +team.getTeamid()+"\">").append(team.getName()).append("</a>")
			.append("&nbsp;<a href=\"javascript:teamInviteAgree("+team.getTeamid()+ "," + message.getMessageid()+")\">").append("同意").append("</a>")
			.append("&nbsp;<a href=\"javascript:teamInviteReject("+team.getTeamid()+ "," + message.getMessageid()+")\">").append("拒绝").append("</a>");
		}else{
			b.append("(已处理)群组邀请：")
			.append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/space/index.do;jsessionid=" + jsessionid +"?usr=" +sender.getUserid()+"\">")
			.append(sender.getNickname())
			.append("</a>")
			.append("邀请您加入群组:").append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/team/index.do;jsessionid=" + jsessionid +"?teamid=" +team.getTeamid()+"\">").append(team.getName()).append("</a>")
			.append("&nbsp;").append(getDeleteHref(message.getMessageid()));
		}
		
		return b;
	}
	/**
	 * 用户申请加入群组时，通知组长/副组长
	 */
	private StringBuilder getTeamJoin(HttpServletRequest req, HttpSession session){
		String jsessionid = session.getId();
		String cpath = req.getContextPath();
		StringBuilder b = new StringBuilder();
		UserManager umanager = (UserManager) ApplicationManager.appContext.getBean("userManager");
		TeamManager tmanager = (TeamManager) ApplicationManager.appContext.getBean("teamManager");
		Team team = (Team) tmanager.get(Team.class, Long.valueOf(message.getContent()));
		if(team.getStatus().compareTo(Team.STATUS_NORMAL) != 0){
			return b;
		}
		UserInfo sender = umanager.getByUserid(message.getSender());
		b.append("群组申请：用户")
		.append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/space/index.do;jsessionid=" + jsessionid +"?usr=" +sender.getUserid()+"\">")
		.append(sender.getNickname())
		.append("</a>")
		.append("申请加入群组：")
		.append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/team/index.do;jsessionid=" + jsessionid +"?teamid=" +team.getTeamid()+"\">").append(team.getName()).append("</a>")
		.append("&nbsp;&nbsp;").append(getDeleteHref(message.getMessageid()));
		return b;
	}
	/**
	 * 组长/副组长 通过用户申请,通知用户
	 */
	private StringBuilder getTeamJoinAccept(HttpServletRequest req, HttpSession session){
		String jsessionid = session.getId();
		String cpath = req.getContextPath();
		StringBuilder b = new StringBuilder();
		UserManager umanager = (UserManager) ApplicationManager.appContext.getBean("userManager");
		TeamManager tmanager = (TeamManager) ApplicationManager.appContext.getBean("teamManager");
		Team team = (Team) tmanager.get(Team.class, Long.valueOf(message.getContent()));
		if(team.getStatus().compareTo(Team.STATUS_NORMAL) != 0){
			return b;
		}
		UserInfo sender = umanager.getByUserid(message.getSender());
		b.append("(通过)群组申请：")
		.append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/space/index.do;jsessionid=" + jsessionid +"?usr=" +sender.getUserid()+"\">")
		.append(sender.getNickname())
		.append("</a>")
		.append("通过您加入群组：")
		.append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/team/index.do;jsessionid=" + jsessionid +"?teamid=" +team.getTeamid()+"\">").append(team.getName()).append("</a>")
		.append("&nbsp;&nbsp;").append(getDeleteHref(message.getMessageid()));
		return b;
	}
	/**
	 * 组长/副组长 通过用户申请,通知用户
	 */
	private StringBuilder getTeamJoinReject(HttpServletRequest req, HttpSession session){
		String jsessionid = session.getId();
		String cpath = req.getContextPath();
		StringBuilder b = new StringBuilder();
		UserManager umanager = (UserManager) ApplicationManager.appContext.getBean("userManager");
		TeamManager tmanager = (TeamManager) ApplicationManager.appContext.getBean("teamManager");
		Team team = (Team) tmanager.get(Team.class, Long.valueOf(message.getContent()));
		if(team.getStatus().compareTo(Team.STATUS_NORMAL) != 0){
			return b;
		}
		UserInfo sender = umanager.getByUserid(message.getSender());
		b.append("(拒绝)群组申请：")
		.append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/space/index.do;jsessionid=" + jsessionid +"?usr=" +sender.getUserid()+"\">")
		.append(sender.getNickname())
		.append("</a>")
		.append("拒绝您加入群组：")
		.append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/team/index.do;jsessionid=" + jsessionid +"?teamid=" +team.getTeamid()+"\">").append(team.getName()).append("</a>")
		.append("&nbsp;&nbsp;").append(getDeleteHref(message.getMessageid()));
		return b;
	}
	
	/**
	 *用户退出,通知用户
	 */
	private StringBuilder getLeaveTeamUser(HttpServletRequest req, HttpSession session){
		String jsessionid = session.getId();
		String cpath = req.getContextPath();
		StringBuilder b = new StringBuilder();
		TeamManager tmanager = (TeamManager) ApplicationManager.appContext.getBean("teamManager");
		Team team = (Team) tmanager.get(Team.class, Long.valueOf(message.getContent()));
		if(team.getStatus().compareTo(Team.STATUS_NORMAL) != 0){
			return b;
		}
		b.append("(退出)群组退出：")
		.append("您已退出群组：")
		.append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/team/index.do;jsessionid=" + jsessionid +"?teamid=" +team.getTeamid()+"\">").append(team.getName()).append("</a>")
		.append("&nbsp;&nbsp;").append(getDeleteHref(message.getMessageid()));
		return b;
	}
	/**
	 *用户退出,通知用户
	 */
	private StringBuilder getTeamPosition(HttpServletRequest req, HttpSession session){
		String jsessionid = session.getId();
		String cpath = req.getContextPath();
		StringBuilder b = new StringBuilder();
		TeamManager tmanager = (TeamManager) ApplicationManager.appContext.getBean("teamManager");
		UserManager umanager = (UserManager) ApplicationManager.appContext.getBean("userManager");
		Team team = (Team) tmanager.get(Team.class, Long.valueOf(message.getContent()));
		if(team.getStatus().compareTo(Team.STATUS_NORMAL) != 0){
			return b;
		}
		UserInfo sender = umanager.getByUserid(message.getSender());
		TeamUser tUser = tmanager.getTeamUser(message.getReceiver(), team.getTeamid());
		String position = "组员";
		if(tUser.getPosition().compareTo(TeamUser.POSITION_CHAIRMAN) == 0){
			position = "组长";
		}else if(tUser.getPosition().compareTo(TeamUser.POSITION_VICE_CHAIRMAN) == 0){
			position = "副组长";
		}
		b.append("群组职位：")
		.append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/space/index.do;jsessionid=" + jsessionid +"?usr=" +sender.getUserid()+"\">")
		.append(sender.getNickname())
		.append("</a>")
		.append("将您在群组：")
		.append("<a class=\"a2\" target=\"_blank\" href=\""+cpath+ "/team/index.do;jsessionid=" + jsessionid +"?teamid=" +team.getTeamid()+"\">").append(team.getName()).append("</a>")
		.append("的职位修改为：").append(position)
		.append("&nbsp;&nbsp;").append(getDeleteHref(message.getMessageid()));
		return b;
	}
	
	private String getDeleteHref(Long messageid){
		StringBuilder b = new StringBuilder();
		b.append("<a class=\"a2\" href=\"javascript:removeSysMessage("+messageid+")\">").append("删除").append("</a>");
		return b.toString();
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}
}
